package mess;

public class LeeCode_82_Failed {
    public static void main(String[] args) {
        //System.out.println(deleteDuplicates(new ListNode(1, new ListNode(2, new ListNode(2, new ListNode(3))))));
        System.out.println(deleteDuplicates(new ListNode(1,new ListNode(2,new ListNode(3,
                new ListNode(3,new ListNode(4,new ListNode(4,new ListNode(5)))))))));
    }

    private static ListNode deleteDuplicates(ListNode head) {
        ListNode index1 = head;
        ListNode index2 = head;
        int count1 = 0;
        int count2 = 0;
        while (index2.next != null) {
            ListNode next = index2.next;
            int nextNum = next.val;
            int thisNum = index2.val;
            if (thisNum == nextNum) {
                index2 = next;
                count2++;
            } else {
                index2 = next;
                while (count1 < count2 && !(index1.val != index1.next.val && index1.next.val == index2.val)){
                    index1 = index1.next;
                    count1++;
                }
                index1.next = index2;
                count2++;
            }
        }
        return head;
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return this.val + " " + next;
        }
    }
}
